package com.wholesale.utils;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.wholesale.R;
import com.wholesale.beans.Order;

public class MyOrderListAdapter extends ArrayAdapter<Order> {

	// ListView中的数据来源
	private ListView listView;

	public MyOrderListAdapter(Context context, ListView listView) {
		super(context, 0);
		this.listView = listView;
	}

	public MyOrderListAdapter(Context context, List<Order> orderList,
			ListView listView) {
		super(context, 0, orderList);
		this.listView = listView;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Activity activity = (Activity) getContext();

		// Inflate the views from XML
		View rowView = convertView;
		MyOrderCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.myorderitem, null);
			viewCache = new MyOrderCache(rowView);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MyOrderCache) rowView.getTag();
		}
		Order order = getItem(position);
		viewCache.getNumberView().setText(order.getOrderSn());
		if (order.getAddTime() != null) {
			// "yyyyy.MMMMM.dd GGG hh:mm aaa" 02001.July.04 AD 12:08 PM
			viewCache.getTimeView().setText(order.getAddTime());
		} else {
			viewCache.getTimeView().setText("");
		}
		viewCache.getTotalView().setText(
				"US$ "
						+ Maths.add(order.getGoodsAmount(), Constants
								.getTwoDecimalFloor(order.getShippingFee())));

		if ("Recognized".equalsIgnoreCase(order.getOrderStatusName())) {
			viewCache.getStatusView().setText("Confirmed");
		} else {
			viewCache.getStatusView().setText("Unconfirmed");
		}
		viewCache.getShouldPayView().setText("US$ " + order.getOrderAmount());
		viewCache.getOperateView().setText(order.getPayStatusName());
		if (order.getPayTime() != null) {
			viewCache.getPaidTimeView().setText(order.getPayTime());
		} else {
			viewCache.getPaidTimeView().setText("");
		}
		return rowView;
	}

	/**
	 * 批量添加数据
	 * 
	 * @param items
	 */
	public void addItems(List<Order> items) {
		for (Order order : items) {
			this.add(order);
		}
	}

}
